package com.csht.service.Impl;

import com.csht.service.CshtNoticeService;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csht.mapper.CshtNoticeMapper;
import com.csht.model.CshtNotice;
import com.csht.page.PagerModel;

/**
 * 消息通知service
 * @author chenhuiyan
 *
 */
@Service
public class CshtNoticeServiceImpl implements CshtNoticeService {
	
	@Autowired
	private CshtNoticeMapper CshtNoticeMapper;

	/**
	 * 根据用户id查询未读消息数量
	 */
	@Override
	public Integer getNoticeNumberByUserId(Integer userId) throws Exception {
		Integer count = CshtNoticeMapper.getNoticeNumberByUserId(userId);
		return count;
	}

	/**
	 * 根据用户id、偏移量、每页总数查询消息通知
	 */
	@Override
	public PagerModel<CshtNotice> getNoticeByUserId(Map<String, Integer> res) throws Exception {
		PagerModel<CshtNotice> pagerModel = new PagerModel<CshtNotice>();
		Integer offset = res.get("offset"); //获取偏移量
		Integer noticePageSize = res.get("noticePageSize"); //获取每页的数量
		//根据用户id、偏移量、每页总数查询消息通知
		List<CshtNotice> cshtNoticeList = CshtNoticeMapper.getNoticeByUserId(res);
		//获取消息总数量
		Integer noticeNumber = CshtNoticeMapper.getNoticeTotalByUserId(res.get("userId"));
		pagerModel.setDates(cshtNoticeList);
		pagerModel.setOffset(offset);
		pagerModel.setNoticePageSize(noticePageSize);
		pagerModel.setTotalSize(noticeNumber);
		return pagerModel;
	}

	/**
	 * 根据用户id查询所有消息数量
	 */
	@Override
	public Integer getNoticeTotalByUserId(Integer userId) throws Exception {
		Integer noticeTotal = CshtNoticeMapper.getNoticeTotalByUserId(userId);
		return noticeTotal;
	}

	/**
	 * 根据消息通知id查询消息
	 */
	@Override
	public CshtNotice getNoticeById(Integer noticeId) throws Exception {
		CshtNotice cshtNotice = CshtNoticeMapper.getNoticeById(noticeId);
		return cshtNotice;
	}

	/**
	 * 根据消息通知id更新已读
	 */
	@Override
	public void updateNoticeIsReadyById(Integer noticeId) throws Exception {
		CshtNoticeMapper.updateNoticeIsReadyById(noticeId);
	}

	/**
	 * 插入记录
	 */
	@Override
	public void insertNotice(CshtNotice cshtNotice) throws Exception {
		CshtNoticeMapper.insertNotice(cshtNotice);
	}

}
